package com.dong.auth.module.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dong.auth.module.user.entity.SysUser;
import com.dong.auth.module.user.mapper.SysUserMapper;
import com.dong.auth.module.user.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dong.auth.security.authentication.Authentication;
import com.dong.auth.security.exception.UsernameNotFoundException;
import com.dong.auth.security.userdetails.DefaultSecurityUser;
import com.dong.auth.security.userdetails.SecurityUser;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhao dong chao
 * @since 2021-10-15 13:01:03
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Override
    public SecurityUser loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = getOne(new QueryWrapper<SysUser>().eq("login_name", username));
        if (null == sysUser){
            return null ;
        }

        return parseToSecurityUser(sysUser);
    }

    private SecurityUser parseToSecurityUser(SysUser sysUser) {
        DefaultSecurityUser securityUser = new DefaultSecurityUser();
        securityUser.setUsername(sysUser.getLoginName());
        securityUser.setPassword(sysUser.getPassword());
        securityUser.setState(0);

        return securityUser;
    }
}
